﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using FileUpload_5.Classes;

namespace FileUpload_5
{
	// Custom class implements the IValueConverter interface.
	public class FileStateConverter : System.Windows.Data.IValueConverter
	{

		#region IValueConverter Members

		public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			Constants.FileStates thistate = (Constants.FileStates)value;

			string state = string.Empty;
			switch (thistate)
			{
				case Constants.FileStates.Pending:
					state = "等待";
					break;
				case Constants.FileStates.Finished:
					state = "已上传";
					break;
				case Constants.FileStates.Error:
					state = "错误";
					break;
				case Constants.FileStates.Uploading:
					state = "上传中";
					break;
				case Constants.FileStates.Deleted:
					state = "已删除";
					break;
				case Constants.FileStates.Pause:
					state = "暂停";
					break;
				case Constants.FileStates.Continue:
					state = "继续";
					break;
			}
			// Return the value to pass to the target.
			return state;
		}

		// ConvertBack is not implemented for a OneWay binding.
		public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new System.NotImplementedException();
		}

		#endregion
	}

	public partial class FileRowControl : UserControl
	{

		private UserFile UserFile
		{
			get
			{
				if (this.DataContext != null)
					return ((UserFile)this.DataContext);
				else
					return null;

			}
		}

		public FileRowControl()
		{
			InitializeComponent();

			this.Loaded += new RoutedEventHandler(FileRowControl_Loaded);
		}

		private void FileRowControl_Loaded(object sender, RoutedEventArgs e)
		{
			//Subscribe to PropertyChanged of the UserFile
			UserFile.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(FileRowControl_PropertyChanged);

			VisualStateManager.GoToState(this, UserFile.State.ToString(), true);
		}

		private void FileRowControl_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "State")
			{
				VisualStateManager.GoToState(this, UserFile.State.ToString(), true);

				//Show grey text when the upload is finished
				if (this.UserFile.State == Constants.FileStates.Finished)
				{
					GreyOutText();
					ShowValidIcon();
				}

				if (UserFile.State == Constants.FileStates.Pause)
				{
					UserFile.PauseUpload();
				}

				if (UserFile.State == Constants.FileStates.Continue)
				{
					this.UserFile.ResumeUpload();
				}

				//Show error message when the upload failed:
				if (this.UserFile.State == Constants.FileStates.Error)
				{
					ErrorMsgTextBlock.Visibility = Visibility.Visible;

					if (!string.IsNullOrEmpty(this.UserFile.ErrorMessage))
						ErrorMsgTextBlock.Text = this.UserFile.ErrorMessage;
				}
			}
			else if (e.PropertyName == "Percentage")
			{
				// if the percentage is decreasing, don't use an animation
				if (UserFile.Percentage < PercentageProgress.Value)
					PercentageProgress.Value = UserFile.Percentage;
				else
				{
					sbProgressFrame.Value = UserFile.Percentage;
					sbProgress.Begin();
				}
			}
		}

		private void ShowValidIcon()
		{
			PercentageProgress.Visibility = Visibility.Collapsed;
			ValidUploadIcon.Visibility = Visibility.Visible;
		}

		private void GreyOutText()
		{
			SolidColorBrush grayBrush = new SolidColorBrush(Colors.Gray);

			FileNameTextBlock.Foreground = grayBrush;
			StateTextBlock.Foreground = grayBrush;
			FileSizeTextBlock.Foreground = grayBrush;

		}

		private void DeleteButton_Click(object sender, RoutedEventArgs e)
		{
			UserFile.IsDeleted = true;

			this.Visibility = Visibility.Collapsed;
		}

		private void PauseButton_Click(object sender, RoutedEventArgs e)
		{
			var btn = sender as Button;

			if (btn != null)
			{
				if (btn.Content.Equals("Img/play.png"))
				{
					//this.UserFile.PauseUpload();
					this.UserFile.State = Constants.FileStates.Continue;

					btn.Content = "Img/pause.png";
					ToolTipService.SetToolTip(btn, "暂停");
				}
				else
				{
					//this.UserFile.ResumeUpload();
					this.UserFile.State = Constants.FileStates.Pause;

					btn.Content = "Img/play.png";
					ToolTipService.SetToolTip(btn, "继续");
				}
			}
		}
	}
}
